package com.kly.user.config;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.kly.user.serviceImpl.dto.MsgSender;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class NacosValueConfig {

    @NacosValue(value = "${server.test:false}", autoRefreshed = true)
    private boolean isServerTest;

    @NacosValue(value = "${msg.sender}", autoRefreshed = true)
    private String msgSender;

    @NacosValue(value = "${feishu.push.switch:true}", autoRefreshed = true)
    private boolean feishuPushSwitch;

    @NacosValue(value = "${traffic.source.enable.switch}", autoRefreshed = true)
    private String trafficSourceEnableSwitch;

    @NacosValue(value = "${feishu.alert.api:https://open.feishu.cn/open-apis/bot/v2/hook/c51e5ac8-6cf1-41cb-9e3c-3d645259103d}", autoRefreshed = true)
    private String feishuWebhookApi;

    @NacosValue(value = "${keep.online.user}", autoRefreshed = true)
    private String keepOnlineUserCodes;
    @NacosValue(value = "${pp.wlm.issue.fix.user}", autoRefreshed = true)
    private String ppWlmIssueFixUserCode;

    @NacosValue(value = "${is.pp.fc.list.related.with.master.switch:false}", autoRefreshed = true)
    private boolean ppFCCareMasterSwitch;

    /**
     * pp-firebase埋点-API地址
     */
    @NacosValue(value = "${pp.firebase.event.tracking.url}", autoRefreshed = true)
    private String ppFirebaseEventTrackingUrl;
    /**
     * pp-firebase埋点-与数据流关联的衡量ID
     */
    @NacosValue(value = "${pp.firebase.event.tracking.measurement.id}", autoRefreshed = true)
    private String ppFirebaseEventTrackingMeasurementId;
    /**
     * pp-firebase埋点- API密钥
     */
    @NacosValue(value = "${pp.firebase.event.tracking.api.secret}", autoRefreshed = true)
    private String ppFirebaseEventTrackingApiSecret;
    /**
     * pp-firebase埋点-客户端唯一标识符
     */
    @NacosValue(value = "${pp.firebase.event.tracking.client.id}", autoRefreshed = true)
    private String ppFirebaseEventTrackingClientId;

    public boolean isPpFCCareMasterSwitch() {
        return ppFCCareMasterSwitch;
    }

    public Map<String, MsgSender> getMsgSender() {
        final List<MsgSender> msgSenders = JSONObject.parseArray(msgSender, MsgSender.class);
        return msgSenders.stream().collect(Collectors.toMap(MsgSender::getAppCode, Function.identity()));
    }

    public boolean isServerTest() {
        return isServerTest;
    }

    public String getTrafficSourceEnableSwitch() {
        return trafficSourceEnableSwitch;
    }

    public boolean feishuPushSwitch() {
        return feishuPushSwitch;
    }

    public String feishuWebhookApi() {
        return feishuWebhookApi;
    }

    public List<String> keepOnlineUserCodes() {
        if (StringUtils.isEmpty(keepOnlineUserCodes)) {
            return Collections.emptyList();
        }

        return Arrays.asList(keepOnlineUserCodes.split(","));
    }

    public String ppWlmIssueFixUserCode() {
        return ppWlmIssueFixUserCode;
    }

    public String ppFirebaseEventTrackingUrl() {
        return String.format(ppFirebaseEventTrackingUrl, ppFirebaseEventTrackingMeasurementId, ppFirebaseEventTrackingApiSecret);
    }

    public String ppFirebaseEventTrackingMeasurementId() {
        return ppFirebaseEventTrackingMeasurementId;
    }

    public String ppFirebaseEventTrackingApiSecret() {
        return ppFirebaseEventTrackingApiSecret;
    }

    public String ppFirebaseEventTrackingClientId() {
        return ppFirebaseEventTrackingClientId;
    }
}
